package com.itheima.goods.controller;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    //查询所有
    @GetMapping("/findAll")
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList);  //brandList是传参
    }

    //根据ID查询品牌
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Brand brand=brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    //新增品牌
    @PostMapping("/add") //类型转换
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"新增成功");

    }

    //修改品牌
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    //删除品牌
    @DeleteMapping(value = "/delete/{id}" )
    public Result delete(@PathVariable Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }


    //条件查询
    @GetMapping(value = "/search" ) //因为是实体类没有的字段所以加此注解
    public Result search(@RequestParam Map searchMap){
        List<Brand> list=brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

}
